<?php

namespace Index\Controller;

use Think\Controller;

/**
 * 说明：公共控制器
 * 作者：王楠
 * 创建日期：2017-09-15
 */
class CommonController extends Controller
{

    private $_callback;

    /**
     * 前置方法
     */
    public function _initialize()
    {
        header('content-type:text/json;charset=utf-8');

        if (!session('admin_id') && !session('admin_username')) {//管理员账号为空就跳到登录页

            exit(header("Location: " . C('LOCALHOST_URL') . "/login.shtml"));
        }
        $this->app_name = empty(session('app_code')) ? C('SYSTEM_NAME_ABBR') : session('app_name');
        $this->app_name2 = empty(session('app_code')) ? C('NUMBER_NAME_PRE') : session('app_name');
        $this->app_code = session('app_code');
        $this->login_type = session('login_type');
        $this->admin_id = session('admin_id');
        $this->admin_uid = session('admin_uid');
        $this->admin_username = session('admin_username');
        $this->admin_role_id = session('admin_role_id');
        $this->admin_role_level = session('admin_role_level');
    }


    /*
     * 获取app列表 app_name  app_flag
     * */

    public function getapplist()
    {

        $data = array();
        if (empty(session('app_code'))) {
            $model = M('chat_systemset');
            $data = $model->where(['app_state' => '1'])->field('app_flag,app_name')->select();
        }

        return $data;
    }

    public function getenumbyarr($arr)
    {
        $enum[''] = '总后台';
        foreach ($arr as $key => $value) {
            $enum[$arr[$key]['app_flag']] = $arr[$key]['app_name'];
        }
        return $enum;
    }

    //获取分销商的code
    public function getFxCode($code = "")
    {

        if ($code == "") {
            $map = array('status' => 0);
        } else {
            $map = array('code' => $code, 'status' => 0);
        }
        $model = M('chat_distribution');
        $data = $model->where($map)->field('name,code')->select();
        return $data;
    }

    /*
     * 签名：吴志强 2018/5/15
     * 创建数据库修改的修改的记录文件
     * $data 数组 条件语句 类似比如$data['id']='5',这样的数组集合
     * $model 表名字 字符串 列： 'admin'
     * $id 数据库搜索的条件 就是记录的id值
     */
    public function logtxtsystem($data, $model, $id = 0)
    {
        $key_arr = array_keys($data);
        $arrlist = M($model)->where("id = '" . $id . "'")->find();
        $ok = 0;
        if ($id) {
            $map = "数据库表 $model 更改了: \r\n id==$id \r\n";
            foreach ($key_arr as $key => $value) {
                if ($data[$value] != $arrlist[$value]) {
                    $map .= $value . "字段, 值:" . $data[$value] . "  原字段值为: $arrlist[$value] \r\n";
                    $ok = 1;
                }
            }
        } else {
            $map = "数据库表 $model 创建了一条数据: \r\n";
            foreach ($key_arr as $key => $value) {
                $map .= $value . "字段, 值:" . $data[$value] . "  \r\n";
            }
            $ok = 1;
        }
        if (!file_exists('./Uploads/logsystem')) {
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir('./Uploads/logsystem');
            chmod('./Uploads/logsystem', 0777);
        }
        $mktxt = date("Y-m-d", time());
        if (!file_exists('./Uploads/logsystem/' . $mktxt)) {
            //检查是否有该文件夹，如果没有就创建，并给予最高权限
            mkdir('./Uploads/logsystem/' . $mktxt);
            chmod('./Uploads/logsystem/' . $mktxt, 0777);
        }
        if ($ok) {
            $timetxt = "\r\n 在" . date("H:i:s", time()) . "时进行了操作: \r\n";
            $txt = "./Uploads/logsystem/" . $mktxt . "/$model.txt";
            $myfile = fopen($txt, "a+");
            fwrite($myfile, $timetxt . $map);
            fclose($myfile);
        }
        return;
    }

}

?>